package com.heatup.apt.management.service.datacenter.impl;

import com.heatup.apt.common.dao.DataCenterDaoMapper;
import com.heatup.apt.common.dao.DataCenterDaterangeMapper;
import com.heatup.apt.common.util.JsonUtils;
import com.heatup.apt.management.service.datacenter.DataCenterService;
import com.heatup.apt.model.datacenter.DataCenterOrderDetailModel;
import com.heatup.apt.model.datacenter.DataCenterOrderIncomeAmtModel;
import com.heatup.apt.model.datacenter.DataCenterSortResultModel;
import com.heatup.apt.model.datacenter.DataCenterVIPDeatilModel;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by Administrator on 2016/10/7.
 */
@Service
public class DataCenterServiceImpl implements DataCenterService {
	
	private Logger logger = LoggerFactory.getLogger(DataCenterServiceImpl.class);
   
    @Autowired
    private DataCenterDaterangeMapper dataCenterDaterangeMapper;
    @Autowired
    private DataCenterDaoMapper dataCenterDaoMapper;

    /**
	 * 查询所有datacenter数据
	 *
     * @author Earvin
     * @param paraMap
     * @return map
     */
	@Override
	public Map<String, Object> countDateCenterService(Map<String, Object> paraMap) {
    	String dayStartDay= (String)paraMap.get("nowDate");
		String lastWeekStartDay = (String)paraMap.get("lastWeekStartDay");
		String  lastWeekEndDay = (String)paraMap.get("lastWeekEndDay");
		String lastMonth = (String)paraMap.get("lastMonth");
		Map<String,Object> resp = new HashMap<String,Object>();
		//订单应收金额汇总
		resp.put("dataByDay",countDataCenterByDay(dayStartDay));
		
		resp.put("weekdaterange", dataCenterDaterangeMapper.queryDateCenterDaterangeByType("2"));
		
		resp.put("monthdaterange", dataCenterDaterangeMapper.queryDateCenterDaterangeByType("1"));
		
		resp.put("dataByWeek",countDataCenterByWeekNew(lastWeekStartDay,lastWeekEndDay));
		
		resp.put("dataByMonth",countDataCenterByMonthNew(lastMonth));
		
		resp.put("dataByAll",countDataCenterByAllNew());
		
		return resp;
	}
	
	
	private Map<String,Object> countDataCenterByWeekNew(String startDay,String endDay){
		Map<String,Object> respMap = new HashMap<String,Object>();
		Integer byWeekCourseCount = dataCenterDaoMapper.queryWeekCourseCount(startDay,endDay);
		respMap.put("byWeekCourseCount", byWeekCourseCount);
		
		List<DataCenterSortResultModel> byWeekCourseCountDetail = dataCenterDaoMapper.queryWeekCourseCountDetail(startDay, endDay);
		respMap.put("byWeekCourseCountDetail", JsonUtils.obj2Json(byWeekCourseCountDetail));
		
		BigDecimal byWeekUserAvgChargeCount =  dataCenterDaoMapper.queryUserAvgChargeCount(startDay,endDay);
		respMap.put("byWeekUserAvgChargeCount", byWeekUserAvgChargeCount);
				
	
		return respMap;
	}
	
	private Map<String,Object> countDataCenterByMonthNew(String lastMonth){
		Map<String,Object> respMap = new HashMap<String,Object>();
		String startDate = lastMonth+"-01";
		Date startDay=null;
		try {
			startDay = new SimpleDateFormat("yyyy-MM-dd").parse(startDate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		Calendar cal = Calendar.getInstance();
		cal.setTime(startDay);
		cal.add(Calendar.MONTH, 1);
		Date endDay = cal.getTime();
		String endDate = new SimpleDateFormat("yyyy-MM").format(endDay)+"-01";
		
		BigDecimal byMonthVipOrderRepeatChargeRate = dataCenterDaoMapper.queryVipOrderRepeatChargeRate(startDate,endDate);
		respMap.put("byMonthVipOrderRepeatChargeRate", byMonthVipOrderRepeatChargeRate);
		
		BigDecimal byMonthUserAvgChargeCount =  dataCenterDaoMapper.queryUserAvgChargeCount(startDate,endDate);
		respMap.put("byMonthUserAvgChargeCount", byMonthUserAvgChargeCount);
		
		BigDecimal byMonthVipAmtByPerMonth = dataCenterDaoMapper.queryVipAmtByPerMonth(startDate, endDate);
		respMap.put("byMonthVipAmtByPerMonth", byMonthVipAmtByPerMonth);
		
		return respMap;
	}
	
	
	private Map<String,Object> countDataCenterByAllNew(){
		Map<String,Object> respMap = new HashMap<String,Object>();
		List<DataCenterSortResultModel> courseSortList =  dataCenterDaoMapper.queryCourseSortList(null,null);
		respMap.put("courseSortList", courseSortList);
		
		List<DataCenterSortResultModel> coachSortList =  dataCenterDaoMapper.queryCoachSortList(null,null);
		respMap.put("coachSortList", coachSortList);
		
		List<DataCenterSortResultModel> teamFullClassRateSort =  dataCenterDaoMapper.queryTeamFullClassRateSort(null, null);
		respMap.put("teamFullClassRateSort", teamFullClassRateSort);
		
		return respMap;
	}
	
	
	
	/**
	 * 按日期区间查询数据中心数据
	 * @param paraMap
	 * @return Map
	 */
	public Map<String,Object> countDateCenterByDaysService(Map<String,Object> paraMap){
		Map<String,Object> resp = new HashMap<String,Object>();
		String startDay =(String) paraMap.get("startDay");
		String endDay =(String) paraMap.get("endDay");
		Map<String,Object> countDataByDays = countDataCenterByDatarange(startDay,endDay);
		resp.put("countDataByDays", countDataByDays);
		return resp;
	}
	
	/**
	 * 按天来统计各项数据的方法
	 * @param startDay
	 * @return
	 */
	private Map<String,Object> countDataCenterByDay(String startDay){
		Map<String,Object> respMap = new HashMap<String,Object>();
		//当日订单总金额
		BigDecimal byDayCountAmt = dataCenterDaoMapper.queryOrderIncomeAmtBy(startDay,startDay,null);
		respMap.put("byDayCountAmt", byDayCountAmt);
		
		Integer byDayOrderCount = dataCenterDaoMapper.queryOrderCountBy(startDay, startDay, null);
		respMap.put("byDayOrderCount", byDayOrderCount);
		
		Integer byDayNewVipCount = dataCenterDaoMapper.queryNweVipCountBy(startDay, startDay, null);
		respMap.put("byDayNewVipCount", byDayNewVipCount);
		
		BigDecimal byDayVIPOrderAmt = dataCenterDaoMapper.queryVIPOrderAmtBy(startDay, startDay, null);
		respMap.put("byDayVIPOrderAmt", byDayVIPOrderAmt);
		
		//modifyed by earvin for  1.4 version
		//付费渗透率
		BigDecimal byDayPayInfiltrationRate = dataCenterDaoMapper.queryPayInfiltrationRate(startDay, startDay);
		respMap.put("byDayPayInfiltrationRate", byDayPayInfiltrationRate);
		
		//会员约课转化率
		BigDecimal byDayVipOrderConversionRate = dataCenterDaoMapper.queryVipOrderConversionRate(startDay, startDay);
		respMap.put("byDayVipOrderConversionRate", byDayVipOrderConversionRate);
		
		//零售复购率
		BigDecimal byDayRetailRepeatChargeRate = dataCenterDaoMapper.queryRetailRepeatChargeRate(startDay, startDay,null);
		respMap.put("byDayRetailRepeatChargeRate", byDayRetailRepeatChargeRate);

		BigDecimal byDayTeamFullClassRate = dataCenterDaoMapper.queryTeamFullClassRate(startDay,startDay);
		respMap.put("byDayTeamFullClassRate", byDayTeamFullClassRate);

		BigDecimal byDayNoVipSlefRepeatChargeRate  = dataCenterDaoMapper.queryNoVipSlefRepeatChargeRate(startDay,startDay);
		respMap.put("byDayNoVipSlefRepeatChargeRate", byDayNoVipSlefRepeatChargeRate);
		
		Integer byDayPayUserCount  = dataCenterDaoMapper.queryPayUserCount(startDay,startDay);
		respMap.put("byDayPayUserCount", byDayPayUserCount);
		
		BigDecimal byDayVipUserSelfOrderRate = dataCenterDaoMapper.queryVipUserSelfOrderRate(startDay,startDay);
		respMap.put("byDayVipUserSelfOrderRate", byDayVipUserSelfOrderRate);
		
		BigDecimal byDayNOVipUserSelfOrderRate = dataCenterDaoMapper.queryNOVipUserSelfOrderRate(startDay,startDay);
		respMap.put("byDayNOVipUserSelfOrderRate", byDayNOVipUserSelfOrderRate);
		
		Integer byDayVipUserCount = dataCenterDaoMapper.queryVipUserCount();
		respMap.put("byDayVipUserCount", byDayVipUserCount);
		
		Integer byDayVipExpiredCount   = dataCenterDaoMapper.queryVipExpiredCountBy(startDay, startDay);
		respMap.put("byDayVipExpiredCount", byDayVipExpiredCount);
		
		return respMap;
	}
	
	
	private Map<String,Object> countDataCenterByDatarange(String startDay,String endDay){
		Map<String,Object> respMap = new HashMap<String,Object>();
		//上周订单总金额
		BigDecimal byLastWeekCountAmt=dataCenterDaoMapper.queryOrderIncomeAmtBy(startDay,endDay,null);
		respMap.put("byLastWeekCountAmt", byLastWeekCountAmt);
	
		Integer byLastWeekOrderCount = dataCenterDaoMapper.queryOrderCountBy(startDay, endDay, null);
		respMap.put("byLastWeekOrderCount", byLastWeekOrderCount);
		
		Integer byLastWeekNewVipCount = dataCenterDaoMapper.queryNweVipCountBy(startDay, endDay, null);
		respMap.put("byLastWeekNewVipCount", byLastWeekNewVipCount);
		
		BigDecimal byLastWeekVIPOrderAmt = dataCenterDaoMapper.queryVIPOrderAmtBy(startDay, endDay, null);
		respMap.put("byLastWeekVIPOrderAmt", byLastWeekVIPOrderAmt);
		
		//付费渗透率
		BigDecimal byDayPayInfiltrationRate = dataCenterDaoMapper.queryPayInfiltrationRate(startDay, endDay);
		respMap.put("byDayPayInfiltrationRate", byDayPayInfiltrationRate);
		
		//会员约课转化率
		BigDecimal byDayVipOrderConversionRate = dataCenterDaoMapper.queryVipOrderConversionRate(startDay, endDay);
		respMap.put("byDayVipOrderConversionRate", byDayVipOrderConversionRate);
		
		//零售复购率
		BigDecimal byDayRetailRepeatChargeRate = dataCenterDaoMapper.queryRetailRepeatChargeRate(startDay, endDay,null);
		respMap.put("byDayRetailRepeatChargeRate", byDayRetailRepeatChargeRate);

		//团课满课率
		BigDecimal byDayTeamFullClassRate = dataCenterDaoMapper.queryTeamFullClassRate(startDay,endDay);
		respMap.put("byDayTeamFullClassRate", byDayTeamFullClassRate);
		
		BigDecimal byDayNoVipSlefRepeatChargeRate  = dataCenterDaoMapper.queryNoVipSlefRepeatChargeRate(startDay,endDay);
		respMap.put("byDayNoVipSlefRepeatChargeRate", byDayNoVipSlefRepeatChargeRate);
		
		Integer byDayPayUserCount  = dataCenterDaoMapper.queryPayUserCount(startDay,endDay);
		respMap.put("byDayPayUserCount", byDayPayUserCount);
		
		BigDecimal byDayVipUserSelfOrderRate = dataCenterDaoMapper.queryVipUserSelfOrderRate(startDay,endDay);
		respMap.put("byDayVipUserSelfOrderRate", byDayVipUserSelfOrderRate);
		
		BigDecimal byDayNOVipUserSelfOrderRate  = dataCenterDaoMapper.queryNOVipUserSelfOrderRate(startDay,endDay);
		respMap.put("byDayNOVipUserSelfOrderRate", byDayNOVipUserSelfOrderRate);
	
		Integer byDayVipExpiredCount   = dataCenterDaoMapper.queryVipExpiredCountBy(startDay, endDay);
		respMap.put("byDayVipExpiredCount", byDayVipExpiredCount);

		
		return respMap;
	}

	/**
	 * 按周查询周用户量与周用户购买次数
	 */
	@Override
	public Map<String, Object> queryDataByWeekRange(String startDate, String endDate) {
		Map<String,Object> respMap = new HashMap<String,Object>();
		
		Integer weekCourseCount = 	dataCenterDaoMapper.queryWeekCourseCount(startDate,endDate);
		respMap.put("weekCourseCount", weekCourseCount);
		
		BigDecimal weekUserAvgChargeCount =	dataCenterDaoMapper.queryUserAvgChargeCount(startDate,endDate);
		respMap.put("weekUserAvgChargeCount", weekUserAvgChargeCount);
		
		return respMap;
	}
	


	/**
	 * 按月统计月会员续费率与月用户平均购买次数
	 */
	public Map<String,Object> queryDataByYearMonth(String yearMonth){
		Map<String,Object> respMap = new HashMap<String,Object>();
		String startDate = yearMonth+"-01";
		Date startDay=null;
		try {
			startDay = new SimpleDateFormat("yyyy-MM-dd").parse(startDate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		Calendar cal = Calendar.getInstance();
		cal.setTime(startDay);
		cal.add(Calendar.MONTH, 1);
		Date endDay = cal.getTime();
		String endDate = new SimpleDateFormat("yyyy-MM").format(endDay)+"-01";
		BigDecimal yearMonthVipOrderRepeatChargeRage =	dataCenterDaoMapper.queryVipOrderRepeatChargeRate(startDate,endDate);
		respMap.put("yearMonthVipOrderRepeatChargeRage", yearMonthVipOrderRepeatChargeRage);
		BigDecimal yearMonthUserAvgChargeCount  =dataCenterDaoMapper.queryUserAvgChargeCount(startDate,endDate);
		respMap.put("yearMonthUserAvgChargeCount", yearMonthUserAvgChargeCount);
		BigDecimal yearMonthVipAmtByPerMonth = dataCenterDaoMapper.queryVipAmtByPerMonth(startDate, endDate);
		respMap.put("yearMonthVipAmtByPerMonth", yearMonthVipAmtByPerMonth);
		
		return respMap;
	}

	/**
	 * 教练重复购买率排名
	 */
	@Override
	public Map<String, Object> queryCoachSortDataByDateRange(String startDate,String endDate) {
		Map<String,Object> respMap = new HashMap<String,Object>();
		List<DataCenterSortResultModel> coachSortData = dataCenterDaoMapper.queryCoachSortList(startDate,endDate);
		respMap.put("coachSortData", coachSortData);
		return respMap;
	}

	/**
	 * 课程重复购买率排名
	 */
	@Override
	public Map<String, Object> queryCourseSortDataByDateRange(String startDate,String endDate) {
		Map<String,Object> respMap = new HashMap<String,Object>();
		List<DataCenterSortResultModel> courseSortData = dataCenterDaoMapper.queryCourseSortList(startDate,endDate);
		respMap.put("courseSortData", courseSortData);
		return respMap;
	}
	
	/**
	 * 课程满课率排序
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	public Map<String,Object> queryTeamFullClassRateSort(String startDate,String endDate){
		Map<String,Object> respMap = new HashMap<String,Object>();
		List<DataCenterSortResultModel> teamFullClassRateSort =  dataCenterDaoMapper.queryTeamFullClassRateSort(startDate, endDate);
		respMap.put("teamFullClassRateSort", teamFullClassRateSort);
		return respMap;
	}

	/**
	 * 查询实收金额明细
	 */
	public Map<String,Object> queryCountAmtDetail(String startDate,String endDate){
		Map<String,Object> respMap = new HashMap<String,Object>();
		List<DataCenterOrderIncomeAmtModel> countAmtDetail = dataCenterDaoMapper.queryOrderDetailIncomeAmtBy(startDate, endDate, null);
		respMap.put("countAmtDetail", countAmtDetail);
		return respMap;
	}
	
	/**
	 * 查询订单数量明细
	 */
	public Map<String,Object> queryOrderCountDetail(String startDate,String endDate){
		Map<String,Object> respMap = new HashMap<String,Object>();
		List<DataCenterOrderDetailModel> orderCountDetail = dataCenterDaoMapper.queryOrderCountDetailBy(startDate, endDate, null);
		respMap.put("orderCountDetail", orderCountDetail);
		return respMap;
	}
	
	/**
	 * 查询新增会员明细
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	public Map<String,Object> queryNewVipCountDeatil(String startDate,String endDate){
		Map<String,Object> respMap = new HashMap<String,Object>();
		List<DataCenterVIPDeatilModel> newVipCountDeatil = dataCenterDaoMapper.queryNweVipCountDetailBy(startDate, endDate, null);
		respMap.put("newVipCountDeatil", newVipCountDeatil);
		return respMap;
	}
	
	/**
	 * 查询团课满课率明细
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	public Map<String,Object> queryTeamFullClassRateDetail (String startDate,String endDate){
		Map<String,Object> respMap = new HashMap<String,Object>();
		//团课满课率明细
		List<DataCenterSortResultModel> teamFullClassRateDetail = dataCenterDaoMapper.queryTeamFullClassRateDetail(startDate, endDate);
		respMap.put("teamFullClassRateDetail", teamFullClassRateDetail);
		return respMap;
	}
	
	/**
	 * 周团课排课量明细
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	public Map<String,Object> queryWeekCourseCountDetail(String startDate,String endDate){
		Map<String,Object> respMap = new HashMap<String,Object>();
		List<DataCenterSortResultModel> weekCourseCountDetail = dataCenterDaoMapper.queryWeekCourseCountDetail(startDate, endDate);
		respMap.put("weekCourseCountDetail", weekCourseCountDetail);
		return respMap;
	}
	/**
	 * 查询零售复购率明细
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	public Map<String,Object> queryRetailRepeatChargeRateDetail(String startDate,String endDate){
		Map<String,Object> respMap = new HashMap<String,Object>();
		//会员零售复购率
		BigDecimal vipRetailRepeatChargeRate = dataCenterDaoMapper.queryRetailRepeatChargeRate(startDate, endDate,"2");
		respMap.put("vipRetailRepeatChargeRate", vipRetailRepeatChargeRate);
		//非会员零售复购率
		BigDecimal noVipRetailRepeatChargeRate = dataCenterDaoMapper.queryRetailRepeatChargeRate(startDate, endDate,"1");
		respMap.put("noVipRetailRepeatChargeRate", noVipRetailRepeatChargeRate);
		return respMap;
	}

	
}
